#include "scenewidget.h"
#include "ui_scenewidget.h"
#include "gameobject.h"
#include <QPainter>
#include <QMouseEvent>
SceneWidget::SceneWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SceneWidget)
{
    ui->setupUi(this);

    _target = RVO::Vector2(width() / 2, height() / 2); // 固定目标位置为屏幕中央
}

SceneWidget::~SceneWidget()
{
    delete ui;
}

void SceneWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    for(const auto& obj : _gameObjects){
        obj->draw(&painter); // 让游戏对象自己画自己
    }

    painter.setPen(QPen(Qt::blue, 2));
    painter.setBrush(Qt::NoBrush);
    painter.drawLine(_target.x()-1, _target.y() - 50, _target.x()-1, _target.y() + 50);
    painter.drawLine(_target.x() - 50, _target.y()-1, _target.x() + 50, _target.y()+1);
    painter.drawEllipse(QPointF(_target.x(), _target.y()), 12, 12);
    painter.drawEllipse(QPointF(_target.x(), _target.y()), 25, 25);
}

void SceneWidget::setGameObjects(std::vector<GameObject *> gameObjects)
{
    _gameObjects = gameObjects;
    for(auto& e : _gameObjects){
        e->setTarget(_target);
    }
}

void SceneWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        _target = RVO::Vector2(event->pos().x(), event->pos().y());
        for(auto& e : _gameObjects) e->setTarget(_target);
    }
}
